package com.jhh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jhh.entity.UserTable;
import com.jhh.service.IUserTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author jhh
 * @since 2024-11-22
 */
@RestController
@RequestMapping("/userTable")
@Api(tags = "用户表管理接口")  // 为整个控制器添加一个API说明
public class UserTableController {

    @Autowired
    private IUserTableService userTableService;

    // 获取所有用户
    @GetMapping("/all")
    @ApiOperation(value = "获取所有用户", notes = "获取数据库中所有用户的列表")
    public List<UserTable> getAllUsers() {
        return userTableService.list();
    }

    // 根据ID获取单个用户
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID获取用户", notes = "根据用户ID获取用户详情")
    public UserTable getUserById(@PathVariable Long id) {
        return userTableService.getById(id);
    }

    // 新增用户
    @PostMapping("/add")
    @ApiOperation(value = "新增用户", notes = "向数据库中添加一个新的用户")
    public boolean addUser(@RequestBody UserTable userTable) {
        return userTableService.save(userTable);
    }

    // 更新用户
    @PutMapping("/update")
    @ApiOperation(value = "更新用户", notes = "根据ID更新用户的信息")
    public boolean updateUser(@RequestBody UserTable userTable) {
        return userTableService.updateById(userTable);
    }

    // 删除用户
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除用户", notes = "根据ID删除指定的用户")
    public boolean deleteUser(@PathVariable Long id) {
        return userTableService.removeById(id);
    }

    // 按名字查询用户
    @GetMapping("/name/{name}")
    @ApiOperation(value = "按用户名查询用户", notes = "根据用户名查询用户列表")
    public List<UserTable> getUserByName(@PathVariable String name) {
        LambdaQueryWrapper<UserTable> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserTable::getUsername, name);
        return userTableService.list(queryWrapper);
    }
}
